<?php
namespace App\Services\Admin;

use App\Repositories\Admin\CompanyRepository;
use App\Repositories\Admin\OrderBaleRepository;
use App\Repositories\Admin\OrderRepository;
use App\Repositories\Admin\OrderTempRepository;
use App\Repositories\Admin\UserRepository;

class OrderTempService
{
    private $service;
    private $bale;
    private $company;
    private $user;
    private $order;

    public function __construct()
    {
        $this->service = new OrderTempRepository();
        $this->bale = new OrderBaleRepository();
        $this->company = new CompanyRepository();
        $this->user = new UserRepository();
        $this->order = new OrderRepository();
    }

    public function getIndex()
    {
        $models = $this->service->getIndexList();
        if (count($models)) {
            foreach ($models as $model) {
                $company = $this->company->getShowOneByUid($model->cid);
                $factory = $this->company->getShowOneByUid($model->fid);
                $bale = $this->bale->getShowOne($model->bale);
                $model->comName = ($company&&$company->com_name) ?
                    $company->com_name : '发单公司';
                $model->facName = ($factory&&$factory->com_name) ?
                    $factory->com_name : '抢单公司';
                $model->baleName = $bale ? $bale->name : '测试货单';
//                $model->periodTime = date('Y-m-d',$bale->period);
                $model->periodTime = ($model&&$model->period>1500000000) ?
                    date('Y-m-d',$model->period) : '测试有效期';
                $model->createTime = date('Y-m-d',$model->created_at);
            }
        }
        return $models;
    }

    /**
     * 通过货单ID，获取工厂列表
     */
    public function getListByBale($bale)
    {
        $models = $this->service->getListByBale($bale);
        if (!count($models)) { return array(); }
        foreach ($models as $model) {
            $factory = $this->company->getShowOneByUid($model->fid);
            $user = $this->user->getShowOne($model->fid);
            $model->facName = $factory ? $factory->name : '测试';
            $model->facHead = $user ? $user->head : '';
            $model->uid = $model ? $model->fid : 0;
            $model->createTime = date('Y-m-d',$model->created_at);
        }
        return $models;
    }

    /**
     * 通过货单ID，选中工厂
     */
    public function setPartner($uid,$fid)
    {
        $model = $this->service->getShowOneByUid($uid,$fid);
        if (!$model) {
            return array('code'=>-1,'msg'=>'记录不存在！');
        }
        if ($model && $model->status==4) {
            return array('code'=>-2,'msg'=>'抢单已完成！');
        }
        $resBale = $this->bale->getShowOne($model->bale);
        if (!$resBale) {
            return array('code'=>-3,'msg'=>'货单不存在！');
        }
        $orderArr = array(
            'bale' => $model->bale,
            'temp' => $model->id,
            'cid' => $uid,
            'fid' => $fid,
            'money' => $resBale->num,
        );
        //加工订单新增
        $resOrder = $this->order->setAdd($orderArr);
        if (!$resOrder) {
            return array('code'=>-4,'msg'=>'加工单新增失败！');
        }
        //更新抢单状态
        $lastId = $this->service->setPartner($uid,$fid);
        return array('code'=>0,'data'=>$lastId);
    }

//    public function getIndexTemp($type,$uid)
//    {
//        $models = $this->service->getIndexTemp($type,$uid);
//        if (!count($models)) { return array(); }
//        foreach ($models as $model) {
//            $bale = $this->bale->getShowOne($model->bale);
//            $company = $this->company->getShowOneByUid($model->cid);
//            $factory = $this->company->getShowOneByUid($model->fid);
//            $model->baleName = $bale ? $bale->name : '测试';
//            $model->baleImg = ($bale&&$bale->img) ? $bale->img : '';
//            $model->comName = $company ? $company->name : '测试';
//            $model->facName = $factory ? $factory->name : '测试';
//            $model->statusName = $this->getStatusName($model->status);
//        }
//        return $models;
//    }

    public function add($data)
    {
        return $this->service->setAdd($data);
    }

    public function modify($data,$id)
    {
        return $this->service->setModify($data,$id);
    }

    public function getOne($id)
    {
        return $this->service->getShowOne($id);
    }

    /**
     * 通过货单ID，获取
     */
    public function getOneByBale($bale)
    {
        $model = $this->service->getShowOneByBale($bale);
        if (!$model) {
            return array('code'=>-1,'msg'=>'记录不存在！');
        }
//        if ($model && $model->status==4) {
//            return array('code'=>-2,'msg'=>'抢单已完成！');
//        }
        $model2 = $this->service->getShowOneByBale2($bale);
        if ($model2) {
            return array('code'=>-2,'msg'=>'抢单已完成！');
        }
        return array(
            'code' => 0,
            'data' => $model,
        );
    }

    public function getOneByComId($cid)
    {
        $model = $this->service->getShowOneByComId($cid);
        $baleModel = $this->bale->getShowOneByCid($model->cid);
        $model->bale = $baleModel;
        return $model;
    }

    public function getOneByFacId($cid)
    {
        $model = $this->service->getShowOneByFacId($cid);
        $baleModel = $this->bale->getShowOneByCid($model->cid);
        $model->bale = $baleModel;
        return $model;
    }

    /**
     * 设置状态
     * type：1公司，2工厂
     */
    public function setStatus($type,$cid,$data)
    {
        if ($type==1) { //公司
            return $this->service->setStatusCom($cid,$data);
        } else {
            return $this->service->setStatusFac($cid,$data);
        }
    }

    /**
     * 状态名称
     */
    public function getStatusName($status)
    {
        return $this->service->getStatusName($status);
    }
}